#include "welcomewindow.h"
#include <QApplication>
#include <QFont>
#include<QMessageBox>


WelcomeWindow::WelcomeWindow(RecordsManager *recordsManager,QWidget *parent)
    : QWidget(parent) ,m_recordsManager(recordsManager)
{

    // 在难度选择框下方添加记录显示
    for(int i=0; i<3; i++) {
        m_bestTimeLabels[i] = new QLabel("最佳记录: N/A");
        m_bestTimeLabels[i]->setStyleSheet("color: #666; font-size: 12px;");
    }

    // 在布局中添加
    QVBoxLayout *layout = new QVBoxLayout(this);
    // ...其他控件...
    layout->addWidget(m_bestTimeLabels[0]);
    layout->addWidget(m_bestTimeLabels[1]);
    layout->addWidget(m_bestTimeLabels[2]);


    // 设置窗口大小
    setFixedSize(300, 300);

    // 创建界面元素
    titleLabel = new QLabel("MyPuzzle");
    startButton = new QPushButton("Start Game");
    difficultyComboBox = new QComboBox();


    difficultyComboBox->addItem("3x3 简单", 3);
    difficultyComboBox->addItem("4x4 中等", 4);
    difficultyComboBox->addItem("5x5 困难", 5);

    difficultyComboBox->setStyleSheet(
        "QComboBox {"
        "  font-size: 14px;"
        "  padding: 5px;"
        "}");

    // 设置样式
    QFont titleFont("Arial", 24, QFont::Bold);
    titleLabel->setFont(titleFont);
    titleLabel->setAlignment(Qt::AlignCenter);

    startButton->setFixedSize(150, 40);
    startButton->setStyleSheet(
        "QPushButton {"
        "  background-color: #4CAF50;"
        "  border: none;"
        "  color: white;"
        "  font-size: 16px;"
        "  border-radius: 5px;"
        "}"
        "QPushButton:hover {"
        "  background-color: #45a049;"
        "}");

    // 布局管理
    layout->addStretch();
    layout->addWidget(titleLabel);
    layout->addSpacing(20);
    layout->addWidget(difficultyComboBox, 0, Qt::AlignCenter);
    layout->addSpacing(20);
    layout->addWidget(startButton, 0, Qt::AlignCenter);
    layout->addStretch();

    connect(startButton, &QPushButton::clicked, this, [this](){
        emit startClicked(this->geometry(), difficultyComboBox->currentData().toInt());  // 传递当前几何信息和难度
        this->hide();
    });

    // 添加难度改变信号
    connect(difficultyComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, [this](){
        emit difficultyChanged(difficultyComboBox->currentData().toInt());
    });

    // 连接记录更新信号到刷新函数
    connect(m_recordsManager, &RecordsManager::recordUpdated, this, [this](int difficulty) {
        updateBestTimeDisplay(difficulty); // 更新特定难度的记录显示
    });



    // 添加清除记录按钮
    clearButton = new QPushButton("清除所有记录");
    clearButton->setStyleSheet(
        "QPushButton { background-color: #ff4444; color: white; padding: 8px; }"
        "QPushButton:hover { background-color: #cc0000; }"
        );

    // 将按钮添加到布局
    layout->addWidget(clearButton, 0, Qt::AlignCenter);

    // 连接按钮点击事件
    connect(clearButton, &QPushButton::clicked, this, [this]() {
        QMessageBox::StandardButton confirm = QMessageBox::question(
            this, "确认", "确定要清除所有记录吗？",
            QMessageBox::Yes | QMessageBox::No
            );
        if (confirm == QMessageBox::Yes) {
            m_recordsManager->clearRecords();
        }
    });



}

void WelcomeWindow::updateBestTimeDisplay(int difficulty) {
    // 根据难度索引更新对应标签（假设3x3=0, 4x4=1, 5x5=2）
    int index = difficulty - 3;
    if (index >= 0 && index < 3) {
        QString bestTime = m_recordsManager->getBestTime(difficulty);
        m_bestTimeLabels[index]->setText("最佳记录: " + bestTime);
    }
}
